﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IterationLife.Extensions.MvcApi;
using IterationLife.Extensions.Qiniu;
using IterationLife.Mobile.AppServices.Dtos.Football;
using IterationLife.Mobile.AppServices.Dtos.UserProfiles;
using IterationLife.Mobile.AppServices.Services.UserProfiles;
using Util;
using Util.Domains.Contexts;
using Util.Exceptions;

namespace IterationLife.Mobile.CampusFootball.Student.ApiGateway.Controllers
{

    public class UserProfileController : MvcApiController
    {
        private IUserProfileService _userProfileService;

        public UserProfileController(IUserProfileService userProfileService)
        {
            _userProfileService = userProfileService;
        }

        [MvcApiAuthorize]
        [HttpGet]
        public ActionResult GetUserProfiles()
        {
            var userId = ApplicationContext.User.UserId.ToGuid();
            UserProfileOutput userProfileOutput = _userProfileService.GetUserProfiles(userId);

            return Ok(userProfileOutput);
        }

        [MvcApiAuthorize]
        [HttpPost]
        public ActionResult EditUserProfiles(EditUserProfileInput input)
        {
            var userId = ApplicationContext.User.UserId.ToGuid();
            _userProfileService.EditUserProfiles(userId, input);
            return Ok();
        }

        [HttpPost]
        public ActionResult UploadAvatar()
        {
            if (Request.Files.Count == 0)
                throw new Warning("获取不到上传的头像文件");
            var file = Request.Files[0];
            var url = _userProfileService.UploadAvatar(file.InputStream, file.ContentLength, file.FileName);
            return Ok(data: url);
        }
        
    }
}